package LeetCode.month01.day30;

import java.util.HashMap;

public class No904 {
    public static void main(String[] args) {
        System.out.println(totalFruit(new int[]{3,3,3,1,2,1,1,2,3,3,}));
    }
    public static int totalFruit(int[] fruits) {
        int n = fruits.length;
        int left = 0;
        int res = 0;
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            map.put(fruits[i],map.getOrDefault(fruits[i],0)+1);
            while(map.size()>2){
                map.put(fruits[left],map.get(fruits[left])-1);
                if(map.get(fruits[left])==0) {
                    map.remove(fruits[left]);
                }
                left++;
            }
            res = Math.max(i-left+1,res);
        }
        return res;
    }
}
